package com.fooddelivery;

import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MemberManagementFrame extends JFrame {
    private JTextArea memberInfoArea;

    public MemberManagementFrame() {
        setTitle("Member Management");
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        memberInfoArea = new JTextArea();
        memberInfoArea.setEditable(false);
        panel.add(new JScrollPane(memberInfoArea), BorderLayout.CENTER);

        loadMembers();

        add(panel);
    }

    private void loadMembers() {
        try (Connection conn = DatabaseConnection.getConnection()) {
            String sql = "SELECT * FROM users WHERE role = 'customer'";
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();

            StringBuilder sb = new StringBuilder();
            while (rs.next()) {
                sb.append("User ID: ").append(rs.getInt("id")).append("\n");
                sb.append("Username: ").append(rs.getString("username")).append("\n");
                sb.append("Role: ").append(rs.getString("role")).append("\n\n");
            }
            memberInfoArea.setText(sb.toString());
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
